package nl.hanze.web.t41.http;

import java.net.*;

public class HTTPListener {
	private int port;
	private static final int PORT_MIN=0;
	private static final int PORT_MAX=65535;	
	private HTTPHandler hh;
	
	public HTTPListener(int port, HTTPHandler hh) throws Exception {
		if (port < PORT_MIN || port > PORT_MAX) 
			throw new Exception("Invalid TCP/IP port, out of range");
		this.port=port;
		this.hh=hh;
	}
	
	public void startUp() throws Exception {
		ServerSocket servsock=new ServerSocket(port);
		while (true) {
			Socket s=servsock.accept();
			hh.handleRequest(s.getInputStream(), s.getOutputStream());
			s.close();
		}
	}
}
